
Uec3.App.Ajax=function(){
	
	//private
	var config={
        title : "提示",
        msg : "进行中,请稍后.....",
        buttons: false,
        closable:false,
        width:200,
        wait:true,
        modal:true	            
    };
	return {
		getAjax:function(conf){		
			var ajax = new Ext.data.Connection();
			if(conf){
				config=Ext.applyIf(config,conf);
			}			
		   	Ext.util.Observable.capture(ajax,function(e,conn,b,c){
				if(e=='beforerequest'){
					Ext.MessageBox.show(config);
				}else if(e=='requestcomplete'||e=='requestexception'){
					Ext.MessageBox.hide();
					if(e=='requestexception')Ext.Msg.alert("警告",'加载失败,网络延迟或断网,请稍后重写打开');
				}
			});
			return ajax;
		}
	}
};
Uec3.App.Uec3Ajax = new Uec3.App.Ajax().getAjax();;
Uec3.CompanyConfig=Ext.extend(Ext.util.Observable,{

	constructor:function(config){
		Ext.apply(this, config);
		//this.items=new Ext.util.MixedCollection(false);
		this.addEvents(
            /**
             * @event beforeaction
             * Fires before any action is performed. Return false to cancel the action.
             * @param {Form} this
             * @param {Action} action The {@link Ext.form.Action} to be performed
             */
            'beforeaction',
            /**
             * @event actionfailed
             * Fires when an action fails.
             * @param {Form} this
             * @param {Action} action The {@link Ext.form.Action} that failed
             */
            'actionfailed',
            /**
             * @event actioncomplete
             * Fires when an action is completed.
             * @param {Form} this
             * @param {Action} action The {@link Ext.form.Action} that completed
             */
            'actioncomplete'
        );
        this.init();
        Ext.form.BasicForm.superclass.constructor.call(this);
	},
	baseParam:'cost',
	insurancePrice:'insurancePrice',
	minPrice:'minPrice',
	retailPrice:'retailPrice',
	publicanLevel:'publicanLevel',
	items:new Ext.util.MixedCollection(false),
	init:function(){
		//this.items.add("");
		this.ajax = Uec3.App.Uec3Ajax;
		
		var record=Ext.data.Record.create( [ 
		{
			name : 'varName',
			type : 'string' 
		}, {
			name : 'varValue',
			type : 'string' 
		}]);
		this.store=new Ext.data.Store({
			proxy : new Ext.data.HttpProxy( {
				url : Uec3.App.Constants.homeUrl+'/companyconfig/base!extList.do'
			}),
			paramNames : {
			},
			reader : new Ext.data.JsonReader( {
				totalProperty : 'totalProperty',
				root : 'root'
			}, record)
		});
		this.store.load();
	},
	save:function(param){
		var _this=this;
		this.ajax.request({
			url : Uec3.App.Constants.homeUrl+'/companyconfig/base!save.do',
			params:{coefficient:Ext.encode(param)},
			success:function(form,action){
				var result=Ext.decode(form.responseText);
				if(result.success=="OK"){
					Ext.each(_this.store.getRange(),function(record){
						for(pro in param){
							if(record.data.varName==pro){
								record.data.varValue=param[pro];
							}
						}			
					});
					Ext.MessageBox.alert("信息","保存成功");
				}else{
					Ext.MessageBox.alert("信息","保存失败");
				}
			}
		});
	},
	getVarValue:function(name){
		var store=this.store;
		var index=Ext.each(store.getRange(),function(record){
			if(record.data.varName==name){
				return false;
			}
		});
		var record=store.getAt(index);
		return record.get("varValue");
	},
	getData:function(){
		var ret={};
		Ext.each(this.store.getRange(),function(record){
			ret[record.data.varName]=record.data.varValue;
		});
		return ret;
	},
	setValuesToForm:function(basicForm){
		if(basicForm){
			var baseParamField=basicForm.findField(this.baseParam);
			if(baseParamField){
				var value=baseParamField.getValue();
				var data=this.getData();
				for(pro in data){
					var field=basicForm.findField(pro);
					if(field){
						field.setValue(Ext.util.Format.round(value*data[pro],2));
					}
				}
			}
		}
	},
	setValuesWithPartProperty:function(basicForm){
		var retailPriceField=basicForm.findField("retailPrice");
		var data=this.getData();
		if(!data[this.retailPrice]){
			return;
		}
		if(retailPriceField){
			var value=retailPriceField.getValue();
			var insurancePriceField=basicForm.findField(this.insurancePrice);
			if(insurancePriceField)insurancePriceField.setValue(Ext.util.Format.round(value*data[this.insurancePrice]/data[this.retailPrice],2));
			var minPriceField=basicForm.findField(this.minPrice);
			if(minPriceField)minPriceField.setValue(Ext.util.Format.round(value*data[this.minPrice]/data[this.retailPrice],2));
			var publicanLevelField=basicForm.findField(this.publicanLevel);
			var pv=parseInt(data[this.publicanLevel]?data[this.publicanLevel]:"1");
			if(publicanLevelField)publicanLevelField.setValue(pv);
		}
	},
	getDefaultData:function(){
		var ret={};
		ret.retailPrice="1.3";
		ret.insurancePrice="1.15";
		ret.minPrice='1.05';
		ret.publicanLevel="1";
		return ret;
	}
});

Uec3.App.companyConfig=new Uec3.CompanyConfig();

